package com.bdv.md.dao.impl;

import java.sql.Timestamp;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import java.util.List;

import org.hibernate.criterion.Criterion;

import org.springframework.stereotype.Repository;

import com.bdv.md.dao.TransHistoryDao;
import com.bdv.md.en.ConstantValues;
import com.bdv.md.model.DmTbl;
import com.bdv.md.model.MapTbl;
import com.bdv.md.model.TransHistory;

@Repository
public class TransHistoryDaoImpl extends AbstractDaoImpl<TransHistory, Integer> implements TransHistoryDao{

	Logger logger = Logger.getLogger(this.getClass());
	
	protected TransHistoryDaoImpl() {
		super(TransHistory.class);
	}
	
	public List<TransHistory> findByTransactionId(String transactionId) {
        
		Criteria criteria = getCurrentSession().createCriteria(TransHistory.class);
    	criteria.add(Restrictions.eq("txId", transactionId));
    	return criteria.list() ;
	}
	
	public List<TransHistory> findByRange(Timestamp start, Timestamp end, String type, String meterId) {

        Criteria criteria = getCurrentSession().createCriteria(TransHistory.class);
        criteria.add(Restrictions.between("requestTime", start, end));
    	criteria.add(Restrictions.eq("type", type)); 
    	criteria.add(Restrictions.eq("meterId", meterId)); 
    	
    	logger.error(start + " - " + end + " - " + type + " - " + meterId);
    	System.out.println(start + " - " + end + " - " + type + " - " + meterId);
        return criteria.list() ;
	}

	public TransHistory getTransHistoryByTxID(String txId) {
		Criterion criterion = Restrictions.eq("txId", txId);
		List<TransHistory> list = findByCriteria(criterion);
		return !list.isEmpty() ? findByCriteria(criterion).get(0) : null;
	}

}
